package com.asia5b.a5mall.shop.fragment

import android.app.Activity
import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.same.dialog.ApplyCateSelectDialog
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.base.SimpleFragment
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.home.CategoryModel
import com.asia5b.a5mall.middleware.model.member.AddressModel
import com.asia5b.a5mall.middleware.utils.*
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.activity.ApplyShopActivity
import com.asia5b.a5mall.shop.adapter.ApplyShopAgreeAdapter
import com.asia5b.a5mall.shop.databinding.FragmentApplystepBinding
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import kotlinx.android.synthetic.main.fragment_applystep.*
import java.lang.Exception

/**
 * Created by zhangyuncai on 2020/6/29.
 */
class ApplyStepFragment : SimpleFragment<SimpleViewModel, FragmentApplystepBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(container: ViewGroup?): FragmentApplystepBinding {
        if (arguments != null && requireArguments().containsKey("type")) {
            type = requireArguments().getInt("type")
        }
        applyShopActivity = mActivity as ApplyShopActivity
        return FragmentApplystepBinding.inflate(layoutInflater, container, false)
    }

    val mDatas = arrayListOf<ApplyStepBean>()
    val mAdapter by lazy { ApplyShopAgreeAdapter(mDatas) }
    val mRecyclerView by lazy { rv_applystep }
    var type = 0
    lateinit var applyShopActivity: ApplyShopActivity
    var uploadImgApplyStepBean: ApplyStepBean? = null
    var selectTimeApplyStepBean: ApplyStepBean? = null

    override fun initView() {
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
        setView()
    }

    fun setView() {
        when (type) {
            0 -> {
                addStep1()
            }
            1 -> {
                addStep2()
            }
            2 -> {
                addStep3()
            }
            3 -> {
                addStep4()
            }
        }
    }

    override fun initEvent() {
    }

    override fun startObserver() {
        applyShopActivity.mViewModel.loadLeftDatasLiveData.observe(this, Observer {
            categoryModelList.clear()
            categoryModelList.addAll(it)
            setCateText()
        })
    }

    val categoryModelList = arrayListOf<CategoryModel>()
    override fun lazyFetchData() {
        loadCateDatas()
    }

    fun loadCateDatas() {
        if (type == 3 && categoryModelList.size == 0 && true) {
            //请求商品分类
            applyShopActivity.mViewModel.loadLeftDatas()

        }
    }

    fun setCateText(): String {
        for (applyStepBean in mDatas) {
            if (TextUtils.equals(applyStepBean.name, getString(R.string.经营类目))) {
                val stringBuffer = StringBuffer()
                applyShopActivity.getNotNullApplyShopResModel()
                        .goods_management_category
                        .split(",")
                        .forEach { idText ->
                            for (categoryModel in categoryModelList) {
                                if (TextUtils.equals("${categoryModel.catId}", idText)) {
                                    stringBuffer.append("${categoryModel.showName()} ")
                                }
                            }
                        }
                applyStepBean.value = stringBuffer.toString().trim()
//                        mAdapter.notifyItemChanged(applyStepBean.index)//todo 不知道为什么,禁止复用后,单独更新index,会新增条目
                mAdapter.notifyDataSetChanged()//所以全部强制刷新
                return applyStepBean.value
            }
        }
        return ""
    }

    /**
     * 添加第一步中的UI数据
     */
    fun addStep1() {
        val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
        mDatas.clear()

        mDatas.add(ApplyStepBean(
                getString(R.string.填写基本信息),
                "",
                "",
                "",
                ApplyStepBean.title
        ))
        mDatas.add(ApplyStepBean(
                getString(R.string.公司名称),
                getString(R.string.请填写公司名称),
                getString(R.string.请填写公司名称),
                applyShopResModel.company_name,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.company_name = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.公司地址),
                getString(R.string.请填写公司地址),
                getString(R.string.请填写公司地址),
                applyShopResModel.company_address,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.company_address = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.公司电话),
                getString(R.string.请填写公司电话),
                getString(R.string.请填写公司电话),
                applyShopResModel.company_phone,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.company_phone = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.公司邮箱),
                getString(R.string.请填写公司邮箱),
                getString(R.string.请填写公司邮箱),
                applyShopResModel.company_email,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.company_email = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.员工总数),
                getString(R.string.请填写公司员工总人数),
                getString(R.string.请填写公司员工总人数),
                applyShopResModel.employee_num,
                ApplyStepBean.edittext,
                false
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.employee_num = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.联系人姓名),
                getString(R.string.请填写公司联系人),
                getString(R.string.请填写公司联系人),
                applyShopResModel.link_name,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.link_name = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.联系人电话),
                getString(R.string.请填写公司联系人电话),
                getString(R.string.请填写公司联系人电话),
                applyShopResModel.link_phone,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.link_phone = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.下一步),
                "",
                "",
                "",
                ApplyStepBean.button
        ).apply {
            this.onClickListener = View.OnClickListener {
                submitSetup1()
            }
        })

        mAdapter.notifyDataSetChanged()

    }

    /**
     * 保存第一步的UI数据
     */
    fun submitSetup1() {
        for (applyStepBean in mDatas) {
            dddLog("applyStepBean:${applyStepBean}")
            when (applyStepBean.name) {
                getString(R.string.下一步),
                getString(R.string.员工总数),
                getString(R.string.填写基本信息) -> {

                }
                else -> {
                    if (TextUtils.isEmpty(applyStepBean.value) && applyStepBean.hongxing) {
                        showMessage(applyStepBean.toast)
                        return
                    }
                }
            }
            applyStepBean.fuzhiOnClickListener?.onClick(null)
            //判断规则
            when (applyStepBean.name) {
                getString(R.string.公司邮箱) -> {
                    if (!WValidatorUtil.isEmail2(applyStepBean.value)) {
                        showMessage("邮箱格式不正确")
                        return
                    }
                }
                getString(R.string.公司电话) -> {
                    if (!WValidatorUtil.isPhone(applyStepBean.value)) {
                        showMessage("请输入固定电话,例如：010-888")
                        return
                    }
                }
                getString(R.string.联系人电话) -> {
                    if (!WValidatorUtil.isPhoneNumber2(applyStepBean.value)) {
                        showMessage("联系人电话格式不正确")
                        return
                    }
                }
            }
            //todo 额外补充
            applyShopActivity.getNotNullApplyShopResModel().reg_money = "0"
        }
        applyShopActivity.putShopsApply("step1")
    }

    /**
     * 添加第二步中的UI数据
     */
    fun addStep2() {
        val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
        mDatas.clear()

        mDatas.add(ApplyStepBean(
                getString(R.string.认证信息),
                "",
                "",
                "",
                ApplyStepBean.title
        ))
        mDatas.add(ApplyStepBean(
                getString(R.string.法定代表人姓名),
                getString(R.string.请填写法定代表人姓名),
                getString(R.string.请填写法定代表人姓名),
                applyShopResModel.legal_name,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.legal_name = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.法定代表人身份证),
                getString(R.string.请填写法定代表人身份证号),
                getString(R.string.请填写法定代表人身份证号),
                applyShopResModel.legal_id,
                ApplyStepBean.edittext
        ).apply {
            this.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.legal_id = this.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.法人身份证电子版),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传法人身份证电子版),
                applyShopResModel.legal_img,
                ApplyStepBean.upload_img
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.legal_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.legal_img = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.营业执照编号),
                getString(R.string.请填写营业执照编号),
                getString(R.string.请填写营业执照编号),
                applyShopResModel.license_num,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.license_num = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.营业执照所在地),
                getString(R.string.请选择营业执照所在地),
                getString(R.string.请选择营业执照所在地),
                applyShopResModel.licenseConcatName,
                ApplyStepBean.select
        ).apply {
            val temp = this
            this.onClickListener = View.OnClickListener {
                InputMethodUtils.closeKeyBoard(mActivity)
                val addressModel = AddressModel()
                try {
                    addressModel.countryName = applyShopResModel.license_province
                    addressModel.countryId = applyShopResModel.license_province_id.toInt()
                    addressModel.provinceName = applyShopResModel.license_city
                    addressModel.provinceId = applyShopResModel.license_city_id.toInt()
                    addressModel.cityName = applyShopResModel.license_county
                    addressModel.cityId = applyShopResModel.license_county_id.toInt()
                    addressModel.townName = applyShopResModel.license_town
                    addressModel.townId = applyShopResModel.license_town_id.toInt()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                var selectRegion2DF = ARouter.getInstance()
                        .build(ActCons.SelectRegion2DF)
                        .withSerializable("selectAddressModel", addressModel)
                        .navigation()
                if (selectRegion2DF == null) {
                    return@OnClickListener
                }
                selectRegion2DF = selectRegion2DF as SimpleDialogFragment<*, *>
                selectRegion2DF.show(childFragmentManager, "selectRegion2DF")
                selectRegion2DF.setSureClick<AddressModel> { selectAddressModel ->
                    if (selectAddressModel != null &&
                            selectAddressModel.countryId > 0 &&
                            selectAddressModel.provinceId > 0 &&
                            selectAddressModel.cityId > 0 &&
                            selectAddressModel.townId > 0
                    ) {
                        applyShopResModel.license_province = selectAddressModel.countryName
                        applyShopResModel.license_province_id = "${selectAddressModel.countryId}"
                        applyShopResModel.license_city = selectAddressModel.provinceName
                        applyShopResModel.license_city_id = "${selectAddressModel.provinceId}"
                        applyShopResModel.license_county = selectAddressModel.cityName
                        applyShopResModel.license_county_id = "${selectAddressModel.cityId}"
                        applyShopResModel.license_town = selectAddressModel.townName
                        applyShopResModel.license_town_id = "${selectAddressModel.townId}"
                        temp.value = applyShopResModel.licenseConcatName
                        mAdapter.notifyDataSetChanged()
                    }
                }
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.license_region = applyShopResModel.licenseRegionId
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.营业执照详细地址),
                getString(R.string.请填写营业执照详细地址),
                getString(R.string.请填写营业执照详细地址),
                applyShopResModel.license_add,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.license_add = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.成立日期),
                getString(R.string.请选择成立日期),
                getString(R.string.请选择成立日期),
                format(applyShopResModel.establish_date),
                ApplyStepBean.select
        ).apply {
            this.onClickListener = View.OnClickListener {
                selectTimeApplyStepBean = this
                selectTime()
            }
            this.fuzhiOnClickListener = View.OnClickListener {
                //establish_date已在选择日期中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.营业执照有效期),
                getString(R.string.请选择开始日期),
                getString(R.string.请选择开始日期),
                format(applyShopResModel.licence_start),
                ApplyStepBean.select
        ).apply {
            this.onClickListener = View.OnClickListener {
                selectTimeApplyStepBean = this
                selectTime()
            }
            this.fuzhiOnClickListener = View.OnClickListener {
                //licence_start已在选择日期中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.至),
                getString(R.string.请选择结束日期),
                getString(R.string.请选择结束日期),
                format(applyShopResModel.licence_end),
                ApplyStepBean.select
        ).apply {
            this.onClickListener = View.OnClickListener {
                selectTimeApplyStepBean = this
                selectTime()
            }
            this.fuzhiOnClickListener = View.OnClickListener {
                //licence_end已在选择日期中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.法定经营范围),
                getString(R.string.请填写法定经营范围),
                getString(R.string.请填写法定经营范围),
                applyShopResModel.scope,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.scope = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.营业执照电子版),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传营业执照电子版),
                applyShopResModel.licence_img,
                ApplyStepBean.upload_img
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.licence_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //licence_img已在上传图片中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.组织机构代码),
                getString(R.string.请填写组织机构代码),
                getString(R.string.请填写组织机构代码),
                applyShopResModel.organization_code,
                ApplyStepBean.edittext,
                false
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.organization_code = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.组织机构代码证电子版),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传组织机构代码电子版),
                applyShopResModel.code_img,
                ApplyStepBean.upload_img,
                false
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.code_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //code_img已在上传图片中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.一般纳税人证明),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传纳税证明),
                applyShopResModel.taxes_img,
                ApplyStepBean.upload_img,
                false
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.taxes_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //taxes_imgg已在上传图片中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.下一步),
                "",
                "",
                "",
                ApplyStepBean.button
        ).apply {
            this.onClickListener = View.OnClickListener {
                submitSetup2()
            }
        })

        mAdapter.notifyDataSetChanged()

    }

    fun submitSetup2() {
        for (applyStepBean in mDatas) {
            dddLog("applyStepBean:${applyStepBean}")
            if (applyStepBean.name != getString(R.string.认证信息) &&
                    applyStepBean.name != getString(R.string.下一步)) {
                if (TextUtils.isEmpty(applyStepBean.value) && applyStepBean.hongxing) {
                    showMessage(applyStepBean.toast)
                    return
                }
                applyStepBean?.fuzhiOnClickListener?.onClick(null)
            }
            when (applyStepBean.name) {
                getString(R.string.法定代表人身份证) -> {
                    if (!WValidatorUtil.isLegalId(applyStepBean.value)) {
                        showMessage("身份证号格式不正确")
                        return
                    }
                }
            }
        }
        applyShopActivity.putShopsApply("step2")
    }

    /**
     * 添加第三步中的UI数据
     */
    fun addStep3() {
        val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
        mDatas.clear()

        mDatas.add(ApplyStepBean(
                getString(R.string.财务资质信息),
                "",
                "",
                "",
                ApplyStepBean.title
        ))
        mDatas.add(ApplyStepBean(
                getString(R.string.银行开户名),
                getString(R.string.请填写银行开户名),
                getString(R.string.请填写银行开户名),
                applyShopResModel.bank_account_name ?: "",
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.bank_account_name = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.公司银行账号),
                getString(R.string.请填写公司银行账号),
                getString(R.string.请填写公司银行账号),
                applyShopResModel.bank_number,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.bank_number = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.开户银行支行名称),
                getString(R.string.请填写开户银行支行名称),
                getString(R.string.请填写开户银行支行名称),
                applyShopResModel.bank_name,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.bank_name = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.开户银行所在地),
                getString(R.string.请选择开户银行所在地),
                getString(R.string.请选择开户银行所在地),
                applyShopResModel.bankConcatName,
                ApplyStepBean.select
        ).apply {
            val temp = this
            this.onClickListener = View.OnClickListener {
                InputMethodUtils.closeKeyBoard(mActivity)
                val addressModel = AddressModel()
                try {
                    addressModel.countryName = applyShopResModel.bank_province
                    addressModel.countryId = applyShopResModel.bank_province_id.toInt()
                    addressModel.provinceName = applyShopResModel.bank_city ?: ""
                    addressModel.provinceId = applyShopResModel.bank_city_id.toInt()
                    addressModel.cityName = applyShopResModel.bank_county
                    addressModel.cityId = applyShopResModel.bank_county_id.toInt()
                    addressModel.townName = applyShopResModel.bank_town
                    addressModel.townId = applyShopResModel.bank_town_id.toInt()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                var selectRegion2DF = ARouter.getInstance()
                        .build(ActCons.SelectRegion2DF)
                        .withSerializable("selectAddressModel", addressModel)
                        .navigation()
                if (selectRegion2DF == null) {
                    return@OnClickListener
                }
                selectRegion2DF = selectRegion2DF as SimpleDialogFragment<*, *>
                selectRegion2DF.show(childFragmentManager, "selectRegion2DF")
                selectRegion2DF.setSureClick<AddressModel> { selectAddressModel ->
                    if (selectAddressModel != null &&
                            selectAddressModel.countryId > 0 &&
                            selectAddressModel.provinceId > 0 &&
                            selectAddressModel.cityId > 0 &&
                            selectAddressModel.townId > 0
                    ) {
                        applyShopResModel.bank_province = selectAddressModel.countryName
                        applyShopResModel.bank_province_id = "${selectAddressModel.countryId}"
                        applyShopResModel.bank_city = selectAddressModel.provinceName
                        applyShopResModel.bank_city_id = "${selectAddressModel.provinceId}"
                        applyShopResModel.bank_county = selectAddressModel.cityName
                        applyShopResModel.bank_county_id = "${selectAddressModel.cityId}"
                        applyShopResModel.bank_town = selectAddressModel.townName
                        applyShopResModel.bank_town_id = "${selectAddressModel.townId}"
                        temp.value = applyShopResModel.bankConcatName
                        mAdapter.notifyDataSetChanged()
                    }
                }
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.bank_region = applyShopResModel.bankRegionId
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.开户银行许可证电子版),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传开户银行许可证电子版),
                applyShopResModel.bank_img,
                ApplyStepBean.upload_img
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.bank_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //bank_img 已在上传图片中赋值
            }
        })

        mDatas.add(ApplyStepBean(
                getString(R.string.支付宝收款账户),
                getString(R.string.请填写支付宝收款账户),
                getString(R.string.请填写支付宝收款账户),
                applyShopResModel.beneficiary,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.beneficiary = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.支付宝开户名),
                getString(R.string.请填写支付宝开户名),
                getString(R.string.请填写支付宝开户名),
                applyShopResModel.beneficiary_real_name,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.beneficiary_real_name = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.税务登记证号),
                getString(R.string.请填写税务登记证号),
                getString(R.string.请填写税务登记证号),
                applyShopResModel.taxes_certificate_num,
                ApplyStepBean.edittext,
                false
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.taxes_certificate_num = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.纳税人识别号),
                getString(R.string.请填写纳税人识别号),
                getString(R.string.请填写纳税人识别号),
                applyShopResModel.taxes_distinguish_num,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.taxes_distinguish_num = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.税务登记证电子版),
                getString(R.string.只能上传JPG_PNG文件),
                getString(R.string.请上传税务登记证电子版),
                applyShopResModel.taxes_certificate_img,
                ApplyStepBean.upload_img
        ).also { temp ->
            temp.onClickListener = View.OnClickListener {
                uploadImgApplyStepBean = temp
                uploadImg()
            }
            temp.deleteImgOnClickListener = View.OnClickListener {
                applyShopResModel.taxes_certificate_img = ""
                temp.value = ""
                mAdapter.notifyDataSetChanged()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //taxes_certificate_img 已在上传图片中赋值
            }

        })
        mDatas.add(ApplyStepBean(
                getString(R.string.下一步),
                "",
                "",
                "",
                ApplyStepBean.button
        ).apply {
            this.onClickListener = View.OnClickListener {
                submitSetup3()
            }
        })


        mAdapter.notifyDataSetChanged()
    }

    fun submitSetup3() {
        for (applyStepBean in mDatas) {
            dddLog("applyStepBean:${applyStepBean}")
            if (applyStepBean.name != getString(R.string.财务资质信息) &&
                    applyStepBean.name != getString(R.string.下一步)) {
                if (TextUtils.isEmpty(applyStepBean.value) && applyStepBean.hongxing) {
                    showMessage(applyStepBean.toast)
                    return
                }
                applyStepBean.fuzhiOnClickListener?.onClick(null)

            }
            //判断规则
            when (applyStepBean.name) {
                getString(R.string.纳税人识别号) -> {
                    if (!WValidatorUtil.isDistinguish(applyStepBean.value)) {
                        showMessage("纳税人识别号格式有误")
                        return
                    }
                }
            }
        }
        applyShopActivity.putShopsApply("step3")
    }

    /**
     * 添加第四步中的UI数据
     */
    fun addStep4() {
        val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
        mDatas.clear()

        mDatas.add(ApplyStepBean(
                getString(R.string.店铺信息),
                "",
                "",
                "",
                ApplyStepBean.title
        ))
        mDatas.add(ApplyStepBean(
                getString(R.string.店铺名称),
                getString(R.string.请填写店铺名称),
                getString(R.string.请填写店铺名称),
                applyShopResModel.shop_name,
                ApplyStepBean.edittext
        ).also { temp ->
            temp.fuzhiOnClickListener = View.OnClickListener {
                applyShopResModel.shop_name = temp.value
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.店铺地址),
                getString(R.string.请选择店铺地址),
                getString(R.string.请选择店铺地址),
                applyShopResModel.shopConcatName,
                ApplyStepBean.select
        ).apply {
            val temp = this
            this.onClickListener = View.OnClickListener {
                InputMethodUtils.closeKeyBoard(mActivity)
                val addressModel = AddressModel()
                try {
                    addressModel.countryName = applyShopResModel.shop_province
                    addressModel.countryId = applyShopResModel.shop_province_id.toInt()
                    addressModel.provinceName = applyShopResModel.shop_city
                    addressModel.provinceId = applyShopResModel.shop_city_id.toInt()
                    addressModel.cityName = applyShopResModel.shop_county
                    addressModel.cityId = applyShopResModel.shop_county_id.toInt()
                    addressModel.townName = applyShopResModel.shop_town
                    addressModel.townId = applyShopResModel.shop_town_id.toInt()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                var selectRegion2DF = ARouter.getInstance()
                        .build(ActCons.SelectRegion2DF)
                        .withSerializable("selectAddressModel", addressModel)
                        .navigation()
                if (selectRegion2DF == null) {
                    return@OnClickListener
                }
                selectRegion2DF = selectRegion2DF as SimpleDialogFragment<*, *>
                selectRegion2DF.show(childFragmentManager, "selectRegion2DF")
                selectRegion2DF.setSureClick<AddressModel> { selectAddressModel ->
                    if (selectAddressModel != null &&
                            selectAddressModel.countryId > 0 &&
                            selectAddressModel.provinceId > 0 &&
                            selectAddressModel.cityId > 0 &&
                            selectAddressModel.townId > 0
                    ) {
                        applyShopResModel.shop_province = selectAddressModel.countryName
                        applyShopResModel.shop_province_id = "${selectAddressModel.countryId}"
                        applyShopResModel.shop_city = selectAddressModel.provinceName
                        applyShopResModel.shop_city_id = "${selectAddressModel.provinceId}"
                        applyShopResModel.shop_county = selectAddressModel.cityName
                        applyShopResModel.shop_county_id = "${selectAddressModel.cityId}"
                        applyShopResModel.shop_town = selectAddressModel.townName
                        applyShopResModel.shop_town_id = "${selectAddressModel.townId}"
                        temp.value = applyShopResModel.shopConcatName
                        mAdapter.notifyDataSetChanged()
                    }
                }
                temp.fuzhiOnClickListener = View.OnClickListener {
                    applyShopResModel.shop_region = applyShopResModel.shopRegionId
                }
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.经营类目),
                getString(R.string.请至少选择一个经营类目),
                getString(R.string.请至少选择一个经营类目),
                "",
                ApplyStepBean.select
        ).apply {
            val temp = this
            this.onClickListener = View.OnClickListener {
                loadCateDatas()
                ApplyCateSelectDialog(mActivity).apply {
                    this.title = temp.hint
                    mDatas.addAll(categoryModelList)
                    this.goods_management_category = applyShopResModel.goods_management_category
                    this.setSureClick<String> {
                        applyShopResModel.goods_management_category = it
                        setCateText()//重新定义分类
//                        mAdapter.notifyDataSetChanged()
                    }
                }.show()
            }
            temp.fuzhiOnClickListener = View.OnClickListener {
                //goods_management_category 已在选择结果中赋值
            }
        })
        mDatas.add(ApplyStepBean(
                getString(R.string.提交认证),
                "",
                "",
                "",
                ApplyStepBean.button
        ).apply {
            this.onClickListener = View.OnClickListener {
                submitSetup4()
            }
        })

        mAdapter.notifyDataSetChanged()

    }

    fun submitSetup4() {
        for (applyStepBean in mDatas) {
            dddLog("applyStepBean:${applyStepBean}")
            if (applyStepBean.name != getString(R.string.店铺信息) &&
                    applyStepBean.name != getString(R.string.提交认证)) {
                if (TextUtils.isEmpty(applyStepBean.value) && applyStepBean.hongxing) {
                    showMessage(applyStepBean.toast)
                    return
                }
                applyStepBean.fuzhiOnClickListener?.onClick(null)
            }
        }
        applyShopActivity.putShopsApply("step4")
    }

    fun format(timeL: String): String {
        if (TextUtils.isEmpty(timeL)) {
            return ""
        }
        return WTimeUtils.format("${timeL}000", WTimeUtils.text_yyyy_MM_dd)
    }

    fun selectTime() {
        InputMethodUtils.closeKeyBoard(mActivity)
        TimePickerBuilder(mActivity) { date, view ->
            val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
            selectTimeApplyStepBean?.let { temp ->
                temp.value = format("${date.time / 1000}")
                when (temp.name) {
                    getString(R.string.成立日期) -> {
                        applyShopResModel.establish_date = "${date.time / 1000}"
                    }
                    getString(R.string.营业执照有效期) -> {
                        applyShopResModel.licence_start = "${date.time / 1000}"
                    }
                    getString(R.string.至) -> {
                        applyShopResModel.licence_end = "${date.time / 1000}"
                    }
                }
                mAdapter.notifyDataSetChanged()
            }
        }.build().show(true)
    }

    /**
     * 上传图片
     */
    fun uploadImg() {
        openPicture(callback = object : OnResultCallbackListener<LocalMedia> {
            override fun onResult(result: MutableList<LocalMedia>?) {
                pictureSelectCallback(result)
            }

            override fun onCancel() {
            }

        })
    }

    private fun pictureSelectCallback(selectList: MutableList<LocalMedia>?) {
        // 图片选择
        if (!selectList.isNullOrEmpty()) {
            val localMedia = selectList[0]
            val path = localMedia.compressPath
            if (!TextUtils.isEmpty(path)) {
                applyShopActivity.mViewModel.uploaderLiveData.observe(this, Observer { uploadImgResUrl ->
                    val applyShopResModel = applyShopActivity.getNotNullApplyShopResModel()
                    uploadImgApplyStepBean?.let { temp ->
                        temp.value = uploadImgResUrl
                        when (temp.name) {
                            getString(R.string.法人身份证电子版) -> {
                                applyShopResModel.legal_img = uploadImgResUrl
                            }
                            getString(R.string.营业执照电子版) -> {
                                applyShopResModel.licence_img = uploadImgResUrl
                            }
                            getString(R.string.组织机构代码证电子版) -> {
                                applyShopResModel.code_img = uploadImgResUrl
                            }
                            getString(R.string.一般纳税人证明) -> {
                                applyShopResModel.taxes_img = uploadImgResUrl
                            }
                            getString(R.string.开户银行许可证电子版) -> {
                                applyShopResModel.bank_img = uploadImgResUrl
                            }
                            getString(R.string.税务登记证电子版) -> {
                                applyShopResModel.taxes_certificate_img = uploadImgResUrl
                            }
                        }
                        mAdapter.notifyDataSetChanged()
                    }
                })
                applyShopActivity.mViewModel.uploader(path)
            }


        }
    }
}